package org.jground.rx.ui.zk.viewControl.extra;

import java.util.Collection;


import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import org.jground.codetable.Codetable;
import org.jground.codetable.CodetableElement;
import org.jground.codetable.DefaultCodetable;
import org.jground.codetable.DefaultCodetableElement;
import org.jground.rx.RXCodetableFactory;
import org.jground.rx.RXSystem;
import org.jground.rx.RXView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.viewControl.UIAttributeViewBase;
import org.jground.rx.ui.zk.viewControl.UIAttributeViewControlFactory;
import org.jground.util.ArrayUtils;
import org.jground.util.L;
import org.jground.util.StringUtils;
import org.jground.zk.zulX.ListboxX;
import org.zkoss.zul.Label;

public class UIAttributeViewVoting extends UIAttributeViewBase {

    private static Log logger = LogFactory.getLog(UIAttributeViewVoting.class);

    
    public static String VOTING_CODETABLE_NAME = "rxx-voting";

    public static class AttName {
        public static String superRoles = "superRoles";
    }

    public static String WIDGET_HINT = "extra.voting";
    static {
        UIAttributeViewControlFactory.registerViewFactory(WIDGET_HINT, new UIAttributeViewVoting());
    }

    public UIAttributeViewVoting createNew(UIFrame uiFrame, RXView moView) {
        return new UIAttributeViewVoting(uiFrame, moView);
    }

    public UIAttributeViewVoting() {}

    private Codetable ct;
    private Label valueLabel;
    private ListboxX allVotesLb;
    private VotingList votingList = new VotingList();
    private boolean hasSuperRoles;

    private UIAttributeViewVoting(UIFrame uiFrame, RXView moView) {
        super(uiFrame, moView);
        L.debug(logger, "codetable name", VOTING_CODETABLE_NAME);
        try {
            ct = RXSystem.getInstance().getCtService().getCodetable(VOTING_CODETABLE_NAME, true);
        } catch (Exception e) {
            logger.error("Did not find codetable for 'voting'!");
            ct = new DefaultCodetable(VOTING_CODETABLE_NAME, e.toString(), new DefaultCodetableElement("--", e
                    .toString()));
        }
        // check superRoles attribute
        String s = moView.getProperty(AttName.superRoles);
        if (StringUtils.isNotBlank(s)) {
            String[] superRoles = ArrayUtils.trim(s.split(","));
            hasSuperRoles = ArrayUtils.isNotEmpty(ArrayUtils.interSection(RXSystem.getRXPrincipalMethods()
                    .getUserRoles(), superRoles));
        }
        if (hasSuperRoles) {
            add(allVotesLb = new ListboxX("user name", "vote"));
        } else {
            add(valueLabel = new Label());
        }
    }

    public void set(String... values) {
        clear();
        String userName = RXSystem.getRXPrincipalMethods().getUserName();
        if (ArrayUtils.isNotEmpty(values) && values.length % 2 == 0) {
            votingList.initByDBValues(values);
            if (hasSuperRoles) {
                for (String un : votingList.keySet()) {
                    allVotesLb.addListItem(un, votingList.getVote(un));
                }
            } else {
                String code = votingList.getVote(userName);
                CodetableElement cte = ct.getElement(code);
                if (cte != null) {
                    valueLabel.setValue(cte != null ? cte.getName() : "");
                }
            }
        }
    }

    public void clear() {
        if (hasSuperRoles) {
            allVotesLb.removeAllListitems();
        } else {
            valueLabel.setValue("");
        }
        votingList.clear();
    }

    public Collection<String> renderToString() {
        super.renderToStringS.clear();
        if (hasSuperRoles) {
            for (String un : votingList.keySet()) {
                renderToStringS.add(un + " : " + votingList.getVote(un));
            }
        } else {
            String userName = RXSystem.getRXPrincipalMethods().getUserName();
            String code = votingList.getVote(userName);
            CodetableElement cte = ct.getElement(code);
            renderToStringS.add(cte != null ? cte.getName() : "");
        }
        return renderToStringS;
    }

}
